#include <iostream>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 1e5+7;
struct node {
	int x,y;
	node(int a, int b) {
		x=a, y=b;
	}
	node() {};
};
bool cmp(node a, node b) {
	int t1=a.x-a.y;
	int t2=b.x-b.y;
	if (t1<=0 && t2>0) return true;
	else if (t1>0 && t2<=0) return false;
	else if (t1<=0 && t2<=0) return a.x < b.x;
	else return a.y > b.y;
}
node arr[MAXN];
char s[5000005];
int main() {
	#ifdef hai
	freopen("in.txt", "r", stdin);
	#endif
	int T;
	cin>>T;
	int n;
	while (T--) {
		cin>>n;
		int cnt=0;
		for (int i=0; i<n; ++i) {
			scanf("%s", s);
			stack<char> sta;
			int len = strlen(s);
			cnt += len;
			for (int j=0; j<len; ++j) {
				if (!sta.empty() && sta.top()=='(' && s[j]==')') {
					sta.pop();
				}
				else {
					sta.push(s[j]);
				}
			}
			int x=0, y=0;
			while (!sta.empty()) {
				if (sta.top()==')') ++x;
				else ++y;
				sta.pop();
			}
			arr[i].x=x; arr[i].y=y;
		}
		sort(arr, arr+n, cmp);
		int x=0, y=0;
		for (int i=0; i<n; ++i) {;
			if (y>arr[i].x) y=y-arr[i].x+arr[i].y;
			else x=arr[i].x-y+x, y=arr[i].y; 
		}
		cout<<cnt-x-y<<endl;
	}
	return 0;
} 
